package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * ClassName: ShoppingCartServiceImpl
 * Package: com.sky.service.impl
 * Description:
 *
 * @Author 龙培
 * @Create 2025/3/13 18:37
 * @Version 1.0
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断新加入的商品再购物车中是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果不存在，则新增
        if (list.isEmpty()) {
            LocalDateTime now = LocalDateTime.now();
            shoppingCart.setCreateTime(now);
            shoppingCart.setNumber(1);
            Long setmealId = shoppingCart.getSetmealId();
            Long dishId = shoppingCart.getDishId();
            if (setmealId != null) {
                Setmeal setmeal = setmealMapper.SelectById(setmealId);
                String image = setmeal.getImage();
                String name = setmeal.getName();
                BigDecimal amount = setmeal.getPrice();
                shoppingCart.setName(name);
                shoppingCart.setImage(image);
                shoppingCart.setAmount(amount);
            }
            if (dishId != null) {
                Dish dish = dishMapper.getById(dishId);
                String image = dish.getImage();
                String name = dish.getName();
                BigDecimal price = dish.getPrice();
                shoppingCart.setName(name);
                shoppingCart.setImage(image);
                shoppingCart.setAmount(price);
            }
            shoppingCartMapper.insert(shoppingCart);
        } else {
            //如果存在，则更新数量和金额
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //判断减去的商品在购物车中是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart = list.get(0);
        Integer number = cart.getNumber();
        if (number == 1) {
            shoppingCartMapper.deleteShoppingCart(cart.getId());
        }
        else{
            cart.setNumber(number - 1);
            shoppingCartMapper.updateNumberById(cart);
        }
    }
}
